package edu.zueb.sport.rest;

import edu.zueb.sport.logic.server.ServerChecker;
import edu.zueb.sport.rest.api.RestResponse;
import edu.zueb.sport.service.PingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jasonfu
 * @date 2024/9/11 14:14
 */
@Tag(name = "Ping", description = "服务检测")
@Slf4j
@RestController
@RequestMapping(PingRestController.PATH_PING)
public class PingRestController {

    public static final String PATH_PING = "/api/v1/ping";

    PingService pingService;

    public PingRestController(PingService pingService) {
        this.pingService = pingService;
    }

    @Operation(summary = "ping", description = "ping 接口，用来检测程序是否保活")
    @ApiResponse(
            responseCode = "200",
            description = "检测HTTP服务是否正常",
            content = {@Content(examples = {@ExampleObject(value = "PONG")})})
    @GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
    public String ping() {
        return "PONG";
    }

    @Operation(summary = "server", description = "server 接口，用来检测依赖的基础服务是否保活")
    @ApiResponse(
            responseCode = "200",
            description = "检测依赖的移除服务是否正常",
            content = {
                @Content(
                        examples = {
                            @ExampleObject(
                                    value =
                                            "{\"retMsg\": \"SUCCESS\",\"data\": [{\"name\": \"MySQL\",\"isActive\": true}]}")
                        })
            })
    @GetMapping("/server")
    public RestResponse<List<ServerChecker.CheckResult>> check() {
        return RestResponse.success(pingService.ping());
    }
}
